package 面试题42_连续子数组的最大和.动态规划实现;

/**
 * @Author ：xu_xiaofeng.
 * @Date ：Created in 10:45 2021/3/16
 * @Description：
 */
public class Solution {
    public int maxSubArray(int[] nums) {

        // 之前子数组中的最大和
        int curSum = 0;
        int result = nums[0];

        for (int num : nums) {
            if (curSum <= 0) {
                curSum = num;
            } else {
                curSum += num;
            }

            result = result > curSum ? result : curSum;
        }

        return result;
    }
}
